import axios from 'axios';
import router from "../router";
import { ElMessage } from "element-plus";

const service = axios.create({
    // process.env.NODE_ENV === 'development' 来判断是否开发环境
    baseURL: 'https://www.psn.asia/manage/',
    timeout: 5000
});

service.interceptors.request.use(
    config => {
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {
        if(response.data.code === 1) {
            return response.data
        }else if(response.data.code === -3 || response.data.code === -6) {
            router.push('/login')
            return Promise.reject(response)
        }else {
            ElMessage.error(response.data.message)
            return Promise.reject(response)
        }
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

export default service;
